package it.polimi.traveldream.web.beans.employee;

import it.polimi.traveldream.ejbclient.dtos.HotelDTO;

import it.polimi.traveldream.ejbclient.eaos.BaseProductServiceEAO;
import it.polimi.traveldream.utils.enums.BaseProductType;
import it.polimi.traveldream.utils.exceptions.JustInUseException;
import it.polimi.traveldream.utils.exceptions.NotCoherentException;
import it.polimi.traveldream.utils.exceptions.NotSuchProductException;
import it.polimi.traveldream.web.beans.shared.FacesContextHandler;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="detailHotel")
@RequestScoped
public class DetailHotelBean {
	@EJB
	private BaseProductServiceEAO bService;
	@ManagedProperty(value="#{listBaseProducts.selectedHotel}")
	private HotelDTO hotel;
	
	public DetailHotelBean() {
	}
	
	public HotelDTO getHotel() {
		return hotel;
	}

	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}

	public String deleteHotel() {
		try {
			bService.deleteProduct(hotel.getId(), BaseProductType.HOTEL);
			return "index";
		} catch (NotSuchProductException e) {
			FacesContextHandler.produceFacesMessage("growl", FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The selected product cannot be reached.");
			return "detailHotel";
		} catch (JustInUseException e) {
			FacesContextHandler.produceFacesMessage("growl", FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "You cannot delete an overpast product or one already made up a travel package.");
			return "detailHotel";
		}
	}
	
	public String modifyHotel() {
		try {
			bService.modifyProduct(hotel);
			return "index";
		} catch (NotCoherentException e) {
			FacesContextHandler.produceFacesMessage(null,FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The product is not coherent. Please check its fields.");
			return "detailHotel";
		} catch (NotSuchProductException e) {
			FacesContextHandler.produceFacesMessage(null,FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The product cannot be reached.");
			return "detailHotel";
		} catch (JustInUseException e) {
			FacesContextHandler.produceFacesMessage(null,FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "You cannot modify an overpast product or one already made up a travel package.");
			return "detailHotel";
		}	
	}
	
}
